package lk.mrt.cse.wihidum.router.processor;

import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.client.Options;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.AxisFault;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.log4j.Logger;
import lk.mrt.cse.wihidum.util.WihidumConstants;
import lk.mrt.cse.wihidum.router.storage.ClientMap;

import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: Wihidum
 * Date: Jun 2, 2010
 * Time: 10:39:46 AM
 * To change this template use File | Settings | File Templates.
 */
public class AdminProcessor
{
    private static final Logger logger=Logger.getLogger(AdminProcessor.class);
    public String insertBroker(String adminAddress, String brokerAddress)
    {
        try {
            ServiceClient serviceClient = new ServiceClient();
            Options options = new Options();
            options.setTo(new EndpointReference(adminAddress));
            options.setAction(WihidumConstants.Operations.INSERT_BROKER);
            serviceClient.setOptions(options);

            OMFactory factory = OMAbstractFactory.getOMFactory();
            OMElement messageElement = factory.createOMElement("BrokerDetails", null);
            OMElement addressElement = factory.createOMElement("Address", null);
            addressElement.setText(brokerAddress);
            messageElement.addChild(addressElement);

            OMElement responseElement = serviceClient.sendReceive(messageElement);
            return responseElement.getText();
        }
        catch (NullPointerException ex)
        {
            return "Error in processing Insert Broker response";
        }
        catch (AxisFault axisFault)
        {
            return "Error in sending Insert Broker request";
        }
    }

    public String changeTopology (String adminAddress, String topology) {
        try {
            ServiceClient serviceClient = new ServiceClient();
            Options options = new Options();
            options.setTo(new EndpointReference(adminAddress));
            options.setAction(WihidumConstants.Operations.CHANGE_TOPOLOGY);
            serviceClient.setOptions(options);

            OMFactory factory = OMAbstractFactory.getOMFactory();
            OMElement messageElement = factory.createOMElement("TopologyDetails", null);
            OMElement addressElement = factory.createOMElement("Topology", null);
            addressElement.setText(topology);
            messageElement.addChild(addressElement);

            OMElement responseElement = serviceClient.sendReceive(messageElement);
            return responseElement.getText();
        }
        catch (NullPointerException ex)
        {
            return "Error in processing Change Topology response";
        }
        catch (AxisFault axisFault)
        {
            return "Error in sending Change Topology request";
        }
    }

    public String registerClient(String name, String password, String age, String country)
    {
        String clientData = name+"#"+age+"#"+country+"#"+password;
        String response = ClientMap.saveClient(name, clientData);
        logger.info(response);
        return response;
    }

    public String getClients()
    {
        String response =  ClientMap.getClientList();
        logger.info(response);
        return response;
    }

    public String getFriends(ArrayList<String> filters)
    {
        String result = "";
        int i = 0;

        Map<String,String> concatMap = new HashMap<String, String> ();
        for (String filter:filters)
        {
            if (concatMap.containsKey(filter.split("#")[0])) {
                String str = concatMap.get(filter.split("#")[0]);
                str = str.concat("-"+filter.split("#")[1]);
                concatMap.put(filter.split("#")[0], str);
            }
            else {
                concatMap.put(filter.split("#")[0], filter);
            }
        }


        for(String filter:concatMap.values())
        {
            String entry = ClientMap.getClient(filter.split("#")[0]);
            logger.info(filter + ":" + entry);
            String[] temp = entry.split("#");
            if(temp != null)
            {
                String resultTerm = filter.split("#")[0].concat("#").concat(temp[1]).concat("#").concat(temp[2]).concat("#"+filter.split("#")[1]);
                if(i == 0)
                {
                  result = resultTerm;
                   i = -1;
                }
                else
                  result = result.concat("@"+resultTerm);
            }
        }
        logger.info(result);
        return result;
    }
}
